package com.wenhuitec.sysui.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;
import com.wenhuitec.sysui.activity.AddFoodSkuActivity;
import com.wenhuitec.sysui.adapter.ShowFoodSkuAdapter;
import com.wenhuitec.sysui.dialog.ShowKuCunDialog;
import com.wenhuitec.sysui.entity.FoodSkuModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 添加菜品时规格视图
 */
public class FoodSkuView extends LinearLayout {

    private EditText mPriceEd;
    private EditText mPackagePriceEd;
    private EditText mBarCodeEd;
    private TextView mKuCunTv;
    private OnClickLin onClickLin;
    private List<FoodSkuModel> mFoodSkuModelList = new ArrayList<>();
    private Context mContext;
    private TableRow mMoreSkuTr;
    private TableLayout mSkuTableLayout;
    private RelativeLayout mSkuListRv;
    private RecyclerView mShowSkuRv;
    private Activity mActivity;
    private int requestCode;

    public void setOnClickLin(OnClickLin onClickLin) {
        this.onClickLin = onClickLin;
    }

    public FoodSkuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_food_sku, this, true);
        mContext = context;
        //region UI初始化
        mPriceEd = (EditText) findViewById(R.id.priceEd);
        mPackagePriceEd = (EditText) findViewById(R.id.packagePriceEd);
        mBarCodeEd = (EditText) findViewById(R.id.barCodeEd);
        mKuCunTv = (TextView) findViewById(R.id.kuCunTv);
        mShowSkuRv = (RecyclerView) findViewById(R.id.showSkuRv);

        mSkuTableLayout = findViewById(R.id.sku_tableLayout);
        mSkuListRv = findViewById(R.id.sku_list_rv);
        TextView addOrEditBut = findViewById(R.id.addOrEditBut);
        mMoreSkuTr = (TableRow) findViewById(R.id.moreSkuTr);
        //endregion

        mKuCunTv.setOnClickListener((v) -> {
            ShowKuCunDialog showKuCunDialog = new ShowKuCunDialog();
            showKuCunDialog.BottomDialog(mContext, kuCunOnSave);
            if (mFoodSkuModelList.size() == 1) {
                showKuCunDialog.setDate(mFoodSkuModelList.get(0).getStock(), mFoodSkuModelList.get(0).getMaxStock());
            }

        });


        //region 添加更多规格
        mMoreSkuTr.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFoodSkuModelList == null) {
                    mFoodSkuModelList = new ArrayList<>();
                }
                if (mFoodSkuModelList.size() == 0) {
                    mFoodSkuModelList.add(new FoodSkuModel());
                }
                Intent intent = new Intent(mContext, AddFoodSkuActivity.class);
                intent.putExtra("FoodSkuModel", (Serializable) mFoodSkuModelList);
                mActivity.startActivityForResult(intent, requestCode);
            }
        });
        //endregion


        //region 多个规格时的修改和新增按钮
        addOrEditBut.setOnClickListener((v) -> {
            Intent intent = new Intent(mContext, AddFoodSkuActivity.class);
            intent.putExtra("FoodSkuModel", (Serializable) mFoodSkuModelList);
            mActivity.startActivityForResult(intent, requestCode);
        });
        //endregion
    }

    public void setRes(Activity activity, int requestCode) {
        this.mActivity = activity;
        this.requestCode = requestCode;
    }


    public void showList(List<FoodSkuModel> foodSkuModelList) {
        mFoodSkuModelList = foodSkuModelList;
        if (mFoodSkuModelList.size() <= 1) {
            //可编辑
            mPriceEd.setText(foodSkuModelList.get(0).getPrice());
            mPackagePriceEd.setText(foodSkuModelList.get(0).getPachagePrice());
            mKuCunTv.setText(String.valueOf(foodSkuModelList.get(0).getStock()));
            mSkuTableLayout.setVisibility(VISIBLE);
            mSkuListRv.setVisibility(GONE);
        } else {
            //不可编辑，显示列表形式
            mSkuTableLayout.setVisibility(GONE);
            mSkuListRv.setVisibility(VISIBLE);
            ShowFoodSkuAdapter showFoodSkuAdapter = new ShowFoodSkuAdapter(mContext, foodSkuModelList);
            mShowSkuRv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器
            mShowSkuRv.setAdapter(showFoodSkuAdapter);
        }
    }

    public List<FoodSkuModel> getmFoodSkuModelList() {
        if (mFoodSkuModelList.size() == 0) {
            return new ArrayList<>();
        } else if (mFoodSkuModelList.size() == 1) {
            mFoodSkuModelList.get(0).setName("");
            mFoodSkuModelList.get(0).setPrice(mPriceEd.getText().toString());
            mFoodSkuModelList.get(0).setPachagePrice(mPackagePriceEd.getText().toString());
        }
        return mFoodSkuModelList;
    }


    public interface OnClickLin {
        void onClick();
    }

    //region 库存弹窗时的保存点击返回
    ShowKuCunDialog.KuCunOnSave kuCunOnSave = new ShowKuCunDialog.KuCunOnSave() {
        @Override
        public void save(String stock, String maxStock, int autoStock) {
            if (mFoodSkuModelList.size() == 1) {
                mFoodSkuModelList.get(0).setStock(Integer.valueOf(stock));
                mFoodSkuModelList.get(0).setMaxStock(Integer.valueOf(maxStock));
                mKuCunTv.setText(stock);
            }
        }
    };
    //endregion
}
